package com.ilovepostcard;

import java.io.File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Toast;

import com.ilovepostcard.controller.UserController;
import com.ilovepostcard.model.User;
import com.ilovepostcard.utils.Constants;
import com.ilovepostcard.utils.ImageUtil;
import com.ilovepostcard.view.EditProfileView;

public class EditProfileActivity extends BaseActivity {
    
    public static final int REQUEST_CODE_CHOOSE_CITY = 2013;
    
    EditProfileView editProfileView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_profile);

        editProfileView = (EditProfileView) findViewById(R.id.edit_profile_view);
        User user = UserController.getDetailUser(this);
        if (user != null)
            editProfileView.setUser(user);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != RESULT_OK)
            return;
        if (requestCode == ImageUtil.CAMERA) {
            File imageFile = new File(Constants.TEMP_CAMERA_IMAGE_PATH);
            if (imageFile.exists())
                ImageUtil.startPhotoZoom(this, Uri.fromFile(imageFile));
            else
                Toast.makeText(this, "拍摄失败", Toast.LENGTH_SHORT).show();
        } else if (requestCode == ImageUtil.GALLERY) {
            ImageUtil.startPhotoZoom(this, data.getData());
        } else if (requestCode == ImageUtil.CLIPRESULT) {
           
            if (data != null) {
                Bundle extras = data.getExtras();
                Bitmap head = extras.getParcelable("data");

                editProfileView.setHeadImage(head);
            }
        } else if (requestCode == REQUEST_CODE_CHOOSE_CITY) {
            if (data != null) {
                String province = data.getStringExtra(PrintChooseCityActivity.KEY_PROVINCE);
                String city = data.getStringExtra(PrintChooseCityActivity.KEY_CITY);
                editProfileView.setLocation(province, city);
            }
        }
    }
    
}
